package com.earthea.application.web.controller.system;

import com.earthea.application.common.annotation.Log;
import com.earthea.application.common.entity.BaseResponse;
import com.earthea.application.common.entity.TableDataInfo;
import com.earthea.application.common.enums.BusinessType;
import com.earthea.application.common.utils.SecurityUtils;
import com.earthea.application.common.utils.poi.ExcelUtil;
import com.earthea.application.system.domain.entity.SysConfig;
import com.earthea.application.system.domain.service.ISysConfigService;
import com.earthea.application.system.entity.request.SysConfigReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 参数配置 信息操作处理
 */
@Slf4j
@RestController
@RequestMapping("/rest/system/config")
public class SysConfigController {

    @Autowired
    private ISysConfigService sysConfigService;

    /**
     * 获取参数配置列表
     */
    @PreAuthorize("@ss.hasPermi('system:config:list')")
    @GetMapping("/list")
    public TableDataInfo<SysConfig> list(SysConfigReq req) {
        return sysConfigService.select(req);
    }

    @Log(title = "参数管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('system:config:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysConfigReq req) {
        TableDataInfo<SysConfig> info = sysConfigService.select(req);
        ExcelUtil.export(response, "参数数据", info.getRows(), SysConfig.class);
    }

    /**
     * 根据参数编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:config:query')")
    @GetMapping(value = "/{configId}")
    public BaseResponse getInfo(@PathVariable Long configId) {
        SysConfig config = sysConfigService.getConfig(configId);
        BaseResponse response = BaseResponse.success();
        response.setData(config);
        return response;
    }

    /**
     * 根据参数键名查询参数值
     */
    @GetMapping(value = "/configKey/{configKey}")
    public BaseResponse getConfigKey(@PathVariable String configKey) {
        SysConfig config = sysConfigService.getConfig(configKey);
        BaseResponse response = BaseResponse.success();
        response.setData(config);
        return response;
    }

    /**
     * 新增参数配置
     */
    @PreAuthorize("@ss.hasPermi('system:config:add')")
    @Log(title = "参数管理", businessType = BusinessType.INSERT)
    @PostMapping
    public BaseResponse add(@Validated @RequestBody SysConfig config) {
        if (!sysConfigService.checkConfigKeyUnique(config.getConfigKey())) {
            return BaseResponse.failed("新增参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        config.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        return sysConfigService.insert(config) > 0 ? BaseResponse.success() : BaseResponse.failed();
    }

    /**
     * 修改参数配置
     */
    @PreAuthorize("@ss.hasPermi('system:config:edit')")
    @Log(title = "参数管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public BaseResponse edit(@Validated @RequestBody SysConfig config) {
        if (!sysConfigService.checkConfigKeyUnique(config.getConfigKey())) {
            return BaseResponse.failed("修改参数'" + config.getConfigName() + "'失败，参数键名已存在");
        }
        config.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        return sysConfigService.update(config) > 0 ? BaseResponse.success() : BaseResponse.failed();
    }

    /**
     * 删除参数配置
     */
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "参数管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{configIds}")
    public BaseResponse remove(@PathVariable List<Long> configIds) {
        return sysConfigService.batchDelete(configIds) > 0 ? BaseResponse.success() : BaseResponse.failed();
    }

    /**
     * 刷新参数缓存
     */
    @PreAuthorize("@ss.hasPermi('system:config:remove')")
    @Log(title = "参数管理", businessType = BusinessType.CLEAN)
    @DeleteMapping("/refreshCache")
    public BaseResponse refreshCache() {
        sysConfigService.resetConfigCache();
        return BaseResponse.success();
    }
}
